<?php
namespace App\Http\Controllers\Home;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;

/**
 * Description of PersonController
 *
 * @author Administrator
 */
class PersonController extends \App\Http\Controllers\HomeController {
    //个人中心首页
    public function index() {
        if (!Session::has('home')) {
            return redirect('home/login/index');
        }
        $user = Session::get('home');
        $assign = compact('user');
        
        return view("home.person.index", $assign);
    }
    
    /**
     * 获取个人推广二维码。
     * @return type
     */
    public function myQrCode() {
        $qrcode         = session('home')->qrcode;
        $appid          = env('WECHAT_APPID');
        $redirect_uri   = urlencode(url('wechat/index/getuinfo').'?refperson='.$qrcode);
        $url = urlencode("https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
        $qrcode = url("home/index/qrcode?url={$url}&size=240&pading=5&font-size=0&label_text=");
        $assign = compact('qrcode');
        return view('home.person.myqrcode', $assign);
    }
    /**
     * 设置个人信息
     * @param Request $request
     * @return type
     */
    public function setInfo(Request $request) {
        if($request->ajax()) {
            $upload = new \App\Models\FileUpload();
            $us = session('home');
            if ($request->hasFile('idfront')) {
                $idfront = $upload->make($request, 'idfront');
                if (is_array($idfront)) {
                    return \Illuminate\Support\Facades\Response::json($idfront);
                }
                $us->idfront = $idfront;
            }
            if ($request->hasFile('idback')) {
               $idback = $upload->make($request, 'idback');
               if (is_array($idback)) {
                    return \Illuminate\Support\Facades\Response::json($idback);
                }
                $us->idback = $idback;
            }
            if ($request->has('name') && $us->isver == 0) {
                $us->name = $request->input('name');
            }
            if ($request->has('cardnumber') && $us->isver == 0) {
                $us->cardnumber = $request->input('cardnumber');
            }
            $us->phone = $request->input('phone');
            $us->save();
            if ($us) {
                refreshSession('home', $us);
                $res = [
                    'status'    => 1,
                    'info'      => '资料完善成功'
                ];
            } else {
                $res = [
                    'status'    => 0,
                    'info'      => '资料添加失败'
                ];
            }
            return \Illuminate\Support\Facades\Response::json($res);
        }
        $user = session('home');
        $assign = compact('user');
        return view('home.person.setinfo', $assign);
    }
    /**
     * 我的代理信息
     * @return type
     */
    public function myPorxy() {
        $user = session('home');
        $assign = compact('user');
        return view('home/person/myproxy', $assign);
    }
    
    /**
     * 我的一级代理人详细信息
     */
    public function primAgency(Request $request, $id) {
        $agencys = \App\Models\User::where('status', 1)->where('pid', $id)->paginate();
        
        $assign = compact('agencys');
        return view('home.person.primagency', $assign);
    }
    /**
     * 二级代理人详细信息
     */
    public function secAgency(Request $request, $id) {
        $agencys = \App\Models\User::where('status', 1)->where('ppid', $id)->paginate();
        $assign = compact('agencys');
        return view('home.person.secagency', $assign);
    }
    /**
     * 我的订单
     * @return type
     */
    public function myOrder(Request $request) {
        $user = session('home');
        $orders = \App\Models\Order::where('uid', $user->id)->whereIn('status', [0, 1, 3, 4])->orderBy('id', 'desc')->get();
        $assign= compact('orders', 'request');
        return view('home.person.myorder', $assign);       
    }
    
    /**
     * 订单详情
     */
    public function orderDetail(Request $request, $id) {
        $user = session('home');
        $order = \App\Models\Order::find($id);
        $url = urldecode($request->input('url'));
        $assign = compact('order', 'url', 'user');
        return view('home.person.orderdetail', $assign);
    }
    /**
     * 订单详情
     */
    public function myOrderDetail(Request $request, $id) {
        $user = session('home');
        $order = \App\Models\Order::find($id);
        $url = urldecode($request->input('url'));
        $assign = compact('order', 'url', 'user');
        return view('home.person.myorderdetail', $assign);
    }
    /**
     * 我的代付订单
     * @return type
     */
    public function myPayagent (Request $request) {
        $user = session('home');
        $orders = "";
        if ($user->phone) {
            $orders=  \App\Models\Order::where('payphone', $user->phone)->whereIn('status', [0,1,3,4])->orderBy('id', 'desc')->get();
        }
        $assign=compact('orders', 'request');
        return view('home.person.mypayagent', $assign);
    }
    
    /**
     * 我的账务
     */
    public function myAccount() {
        $user = session('home');
        $accounts = \App\Models\Account::where('status', 1)->where('uid', $user->id)->orWhere(function($query){
            $query->where('status', 2)->where('pushstatus', 2);
        })->get();
        $sr = \App\Models\Account::where('status', 1)->where('uid', $user->id)->sum('money');
        $zc = \App\Models\Account::where('status', 2)->where('pushstatus', 2)->where('uid', $user->id)->sum('money');
        $total = sprintf('%0.2f', ($sr-$zc));
        $assign = compact('accounts', 'total');
        return view('home.person.myaccount', $assign);
    }
    
    /**
     * 提现申请
     */
    public function pushMoney() {
        $user = session('home');
        $accounts = \App\Models\Account::where('status', 2)->where('uid', $user->id)->get();
        $sr = \App\Models\Account::where('status', 1)->where('uid', $user->id)->sum('money');
        $zc = \App\Models\Account::where('status', 2)->whereIn('pushstatus', [1,2])->where('uid', $user->id)->sum('money');
        $total = sprintf('%0.2f', ($sr-$zc));
        $assign = compact('accounts', 'total');
        return view('home.person.pushmoney', $assign);
    }
    /**
     * 提现申请
     * @param Request $request
     * @return type
     */
    public function apply(Request $request) {
        $user = session('home');
        $sr = \App\Models\Account::where('status', 1)->where('uid', $user->id)->sum('money');
        $zc = \App\Models\Account::where('status', 2)->whereIn('pushstatus', [1,2])->where('uid', $user->id)->sum('money');
        $total = $sr-$zc;
        if($request->ajax()) {
            if (!$user->bankid || !$user->banktype || !$user->bankaddress || !$user->name) {
                return \Illuminate\Support\Facades\Response::json([
                    'status'    => 0, 
                    'info'      => '请在设置中完善个人资料'
                ]);
            }
            $data['money'] = $request->input('pushmoney');
            if($data['money'] > $total || $data['money'] == 0) {
                return \Illuminate\Support\Facades\Response::json([
                    'status'    => 0, 
                    'info'      => '非法操作'
                ]);
            }
            $data['status'] = 2;
            $data['uid']    = $user->id;
            $data['time']   = time();
            $data['pushstatus']=1;
            $res = \App\Models\Account::create($data);
            return \Illuminate\Support\Facades\Response::json([
                    'status'    => 1, 
                    'info'      => '申请已提交，请等待审核'
                ]);
        }
        $assign = compact('total');
        return view('home.person.apply', $assign);
    }
    /**
     * 关闭订单
     * @param type $id
     * @return type
     */
    public function closeOrder($id) {
        $res = \App\Models\Order::where('id', $id)->update(['status'=>2]);
        return redirect(url('home/person/index'));
    }
    
    public function editOrder(Request $request, $id=null) {
        if ($request->ajax()) {
            $res = \App\Models\Order::where('order', "{$request->input('order')}")->update(['payphone' => "{$request->input('payphone')}"]);
            if ($res) {
                return \Illuminate\Support\Facades\Response::json([
                    'status'    => 1,
                    'info'      => "申请代付成功"
                ]);
            } else {
                return \Illuminate\Support\Facades\Response::json([
                   'status'    => 1,
                   'info'      => "申请代付失败请重新提交订单" 
                ]);
            }
            
        } else {
            $result = \App\Models\Order::find($id);
        }
        $assign = compact('result');
        return view('home.person.payagent', $assign);
    }
}
